const getText = require("./src/getText");
const qs = require('querystring');
const http = require('http');
const fs = require('fs');

const server = http.createServer(async function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*");
    if (req.url === '/') {
        fs.readFile('index.html', function (err, data) {
            res.write(data); // write a response to the client
            res.end(); // end the response
        });
    } else if (req.url === '/getText') {
        let body = '';
        req.on('data', (chunk) => {
            body += chunk;
        });
        req.on('end', async () => {
            const queryObject = qs.parse(body);
            let data = JSON.parse(Object.keys(queryObject)[0])
            let txt = await getText(data.date)
            res.write(txt);
            res.end();
        });
    }
});

server.listen(81);
console.log('server is listening 81')